package org.skydiveapp;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.util.FileManager;
import org.skydiveapplibrary.util.StorageTransferTask;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;

import android.view.ContextThemeWrapper;

@SuppressWarnings("deprecation")
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
	private boolean rebootRequired;
	private SettingsRepository settingsRepository;
	private boolean ignorePreferenceChange;
	
	// custom import task
	private class mStorageTransferTask extends StorageTransferTask
	{
		public mStorageTransferTask()
		{
			super(SettingsActivity.this);
		}
		
		@Override
		protected void onPostExecute(Integer result)
		{
			super.onPostExecute(result);

			if (result == StorageTransferTask.SUCCESS)
				rebootRequired = true;
			else
				revertStorageSettingChange();
		}
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {        
        super.onCreate(savedInstanceState);
        
        settingsRepository = new SettingsRepository(this);
        
        addPreferencesFromResource(R.xml.preferences);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    	
    	if (!FileManager.isExternalStorageAvailable())
    	{
    		Preference storagePreference = findPreference(Constants.PREF_STORAGE_LOCATION);
    		storagePreference.setEnabled(false);
    	}
    }
    
	@Override
    public void onPause()
    {
    	super.onPause();
    	getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
    
	@Override
    public void onBackPressed()
	{
		close();
    }
    
	private void close()
	{
		if (rebootRequired)
		{
			finish();
		    Intent intent = new Intent(this, SkyDiveLog2.class);
		    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
		    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		    startActivity(intent);
		}
		
		finish();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
	{
		if (ignorePreferenceChange)
		{
			ignorePreferenceChange = false;
			return;
		}
		
		if (key.equals(Constants.PREF_STORAGE_LOCATION))
		{			
			settingsRepository.ReloadSettings();
			showStorageTransferWarning();
		}
		else if (key.equals(Constants.PREF_THEME))
		{
			rebootRequired=true;
		}
		
	}
	
	private void showStorageTransferWarning()
	{
		// create alert dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.DialogStyle));
		if (settingsRepository.getUseExternalStorage())
		{
			builder.setMessage(R.string.settings_storage_transfer_to_internal_warning);
		}
		else
		{
			builder.setMessage(R.string.settings_storage_transfer_to_external_warning);
		}

		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.settings_transfer_confirmation);
		
		builder.setPositiveButton(R.string.yes_button, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int id)
			{
				doStorageTransfer();
			}
		});
		builder.setNeutralButton(R.string.no_button, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int id)
			{
				revertStorageSettingChange();
			}
		});
		builder.setOnCancelListener(new DialogInterface.OnCancelListener()
		{
		    public void onCancel(DialogInterface dialog)
		    {
		    	revertStorageSettingChange();
		    }
		});
			
		AlertDialog dialog = builder.create();
		// show dialog
		dialog.show();
	}
	
	private void revertStorageSettingChange()
	{
		ignorePreferenceChange = true;
		settingsRepository.setUseExternalStorage(!settingsRepository.getUseExternalStorage());
		SettingsActivity.this.getPreferenceScreen().removeAll();
		addPreferencesFromResource(R.xml.preferences);
	}
	
	private void doStorageTransfer()
	{
		mStorageTransferTask task = new mStorageTransferTask();
		task.execute();
	}
}